import {  Message } from 'element-ui'
import router from '@/router/index';
const state = {
    token: localStorage.getItem("token") || "",
    isMember:false,
    userName:localStorage.getItem("userName") || ""
};
const getters = {
    // !!将state.token强制转换为布尔值，若state.token存在且不为空(已登录)则返回true，反之返回false
    isLoginedIn: (state) => !!state.token,
};
const mutations = {
    SET_TOKEN: (state, {token,userName}) => {
        state.token = token;
        state.userName = userName
        console.log(state);
    },
    SET_MEMBER:(state,member)=>{
        state.isMember = member
    }
};

const actions = {
    login({
        commit
    }, userInfo) {
        return new Promise((resolve, reject) => {      
            setTimeout(() => {
                commit("SET_TOKEN",{token:"qxs3121dvddf2frg",userName:"宴无师"})
                localStorage.setItem("token","qxs3121dvddf2frg")
                localStorage.setItem("userName","宴无师")
                Message({
                    message: '登录成功',
                    type: 'success'
                  });
                resolve("登录成功！")
            }, 1000);
        })
    },
    loginOut(context){
        console.log(context);
        context.commit("SET_TOKEN",{token:"",userName:""})
        Message({
            message: '已退出登录',
            type: 'warning'
          });
        router.replace({
            path: '/Login',
            query: {
                redirect: router.currentRoute.fullPath
            }
        });
        localStorage.removeItem("token")
        localStorage.setItem("userName","")
    },
    changeMember({commit},status){
        console.log(status);
        commit("SET_MEMBER",status)
    }
};

//   const actions = {
//     logins({ commit }, userInfo) {
//       //const { username, password, platformCode, roleUuid } = userInfo;
//       return new Promise((resolve, reject) => {
//         this.$axios.get('/user?ID=12345')
//             .then(function (response) {
//               console.log(response);
//             })
//             .catch(function (error) {
//               console.log(error);
//             });
//     }
//   };
// }
export default {
    namespaced: true,
    getters,
    state,
    mutations,
    actions,
};